﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
   public class ClsRoleMaster
    {
        public string Role { get; set; }
        public int RoleID { get; set; }
       
        public DataTable GetAllRole()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from Role", "Role");
            }
        }
        public void GetRole(int intRoleID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from Role where RoleID=" + intRoleID.ToString(), "Role");
                if (dt.Rows.Count > 0)
                {
                    RoleID = Convert.ToInt32(dt.Rows[0]["RoleID"]);
                    Role = dt.Rows[0]["Role"].ToString();
                }
            }
        }
        public bool SaveRole(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from Role where Role='" + Role + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Role Name already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into Role (Role) values('" + Role + "')");
                    }

                }
                else
                {
                    string count = data.GetValue("select count(*) from Role where Role='" + Role + "' and RoleID NOT IN (" + RoleID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Role Name already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update Role set Role = '" + Role + "' where RoleID=" + RoleID.ToString());
                    }
                }
            }
            return true;
        }
        public void DeleteRole()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  Role where RoleID=" + RoleID.ToString());
            }
        }
    }
}
